package cn.hzking.gjmall.net.intercept;

import android.content.Context;
import android.util.Log;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;

import cn.hzking.gjmall.app.Gjmall;
import cn.hzking.gjmall.net.RetrofitFactory;
import cn.hzking.gjmall.utils.Logger;
import cn.hzking.gjmall.utils.SPUtils;
import cn.hzking.gjmall.utils.UIUtils;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;

/**
 * @author yemao
 * @date 2017/4/9
 * @description 拦截器工具类!
 */

public class InterceptorUtil {
    public static String TAG = "----";
    private static String Token = "";
    public final static Charset UTF8 = Charset.forName("UTF-8");

    //日志拦截器
    public static HttpLoggingInterceptor LogInterceptor() {
        return new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Log.w(TAG, "log: " + message);
            }
        }).setLevel(HttpLoggingInterceptor.Level.BODY);//设置打印数据的级别
    }
    /**
     * token验证的拦截器1
     * @return
     */
    public static void tokenInterceptor1(){
        new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                //拿到请求体,并添加header携带上token
                Request mRequest = chain.request().newBuilder()
                        .addHeader("Token", Token)
                        .build();
                //拿到响应体
                Response mResponse = chain.proceed(mRequest);

                if (mResponse.code()==201){
                    //重新获取新token
                    //这用了一个特殊接口来获取新的Token
                    Call<String> call = RetrofitFactory.getInstant().API().loginByToken("123456", Token);
                    //拿到请求体
                    Request tokenRequest = call.request();
                    //获取响应体
                    Response tokenResponse = chain.proceed(tokenRequest);
                    //我这假设新的token是在header里返回的
                    //我们拿到新的token头
                    List<String> listHeader = tokenResponse.headers().values("Token");
                    if (listHeader != null) {
                        //重新赋值到新的token
                        Token = listHeader.get(0);
                    }

                    //这是只需要替换掉之前的token重新请求接口就行了
                    Request newRequest = mRequest.newBuilder()
                            .header("Token", Token)
                            .build();
                    return chain.proceed(newRequest);
                }
                return mResponse;
            }
        };
    }

    /**
     * token验证的拦截器
     * @return
     */
    public static Interceptor tokenInterceptor(final Context context) {
        return new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                /**
                 * 1.拦截到返回的数据
                 * 2.判断token是否失效
                 * 3.失效获取新的token
                 * 4.重新请求接口
                 */
                //拿到请求体,并添加header携带上token
                Request mRequest = chain.request().newBuilder()
                        .addHeader("tokenapp", SPUtils.getToken(context)+"")
                        .addHeader("User-Agent", UIUtils.getCurrentUserAgent(Gjmall.getApplicationContext())+"")
                        .addHeader("uuid",UIUtils.getDiviceId())//设备的唯一标识
                        .addHeader("shopId",SPUtils.getShopId(context)+"" )
                        .addHeader("s_USER_ID",SPUtils.getUserId(context)+"")
                        .build();

                Logger.e("token"+SPUtils.getToken(context));
                Logger.e("User-Agent"+UIUtils.getCurrentUserAgent(Gjmall.getApplicationContext()));
                Logger.e("shopId"+SPUtils.getShopId(context));
                Logger.e("s_USER_ID"+SPUtils.getUserId(context));
                //拿到响应体
                Response mResponse = chain.proceed(mRequest);

                Logger.e(mRequest.toString());
                return mResponse;
            }
        };

    }
}
